﻿Imports System.IO
Public Class Pappier
    Public id As Integer
    Public Nom As String
    Public Nombre As Integer
    Public Remarque As String
    'Les constructeur ---------------------------


    Sub New(ByVal id As Integer, ByVal Nom As String, ByVal Nombre As Integer, ByVal Remarque As String)
        Me.id = id
        Me.Nom = Nom
        Me.Nombre = Nombre
        Me.Remarque = Remarque
    End Sub


    Sub New()

    End Sub


    'les listes ---------------------------------

    Public List_jouiture As New ArrayList






    'les fonctions ---------------------------

    Public Sub remplir_List_jouiture()
        Me.List_jouiture.Clear()
        Dim cmd As New SqlClient.SqlCommand("select * from pieces_jointes where Id_Papier=" & Me.id, Partager.con)
        Partager.open()
        Partager.dr = cmd.ExecuteReader
        While Partager.dr.Read
            Dim Piece As New Jouinture(Partager.dr("id"), Partager.dr("nom"), Partager.dr("ex"))
            Me.List_jouiture.Add(Piece)
        End While
        Partager.dr.Close()
        Partager.close()
    End Sub

    Public Sub suprimmer_jouiture(ByVal id As Integer)

        Dim cmd As New SqlClient.SqlCommand("delete from pieces_jointes where Id_Papier=" & id, Partager.con)

        Partager.open()
        cmd.ExecuteNonQuery()
        Partager.close()

        For Each piece As Jouinture In Me.List_jouiture
            My.Computer.FileSystem.DeleteFile(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "PieceJointe\" & piece.Nom & piece.Id & "." & piece.ex)
        Next

        Me.remplir_List_jouiture()


    End Sub

    Public Sub suprimmer_jouitureDirect(ByVal Piece As Jouinture)

        Dim cmd As New SqlClient.SqlCommand("delete from pieces_jointes where Id=" & Piece.Id, Partager.con)

        Partager.open()
        cmd.ExecuteNonQuery()
        Partager.close()
        My.Computer.FileSystem.DeleteFile(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "PieceJointe\" & Piece.Nom & Piece.Id & "." & Piece.ex)
        Me.remplir_List_jouiture()


    End Sub

    Public Sub Ajouter_jouiture(ByVal Piece As Jouinture)

        Dim cmd As New SqlClient.SqlCommand("insert into pieces_jointes values (@Id_Papier,@Nom,@Remarque,GETDATE())", Partager.con)

        cmd.Parameters.Add("@Id_Papier", SqlDbType.Int).Value = Me.id
        cmd.Parameters.Add("@Nom", SqlDbType.VarChar).Value = Piece.Nom
        cmd.Parameters.Add("@Remarque", SqlDbType.VarChar).Value = Piece.ex



        Partager.open()
        cmd.ExecuteNonQuery()

        Partager.close()

        Me.remplir_List_jouiture()


    End Sub

    'Public Sub modifier_jouiture(ByVal Piece As Jouinture, ByVal idd As Integer)


    '    Dim cmd As New SqlClient.SqlCommand("update  pieces_jointes set Id_Papier=@Id_Papier,Nom=@Nom , Remarque=@Remarque where id=" & idd & ")", Partager.con)

    '    cmd.Parameters.Add("@Id_Papier", SqlDbType.VarChar).Value = Me.id
    '    cmd.Parameters.Add("@Nom", SqlDbType.VarChar).Value = Piece.Nom
    '    cmd.Parameters.Add("@Remarque", SqlDbType.VarChar).Value = Piece.ex
    '    File.WriteAllBytes("PieceJointe\" & Piece.Nom & Piece.Id & "." & Piece.ex, Piece.fichier)
    '    Partager.open()
    '    cmd.ExecuteNonQuery()
    '    Partager.close()
    '    Me.remplir_List_jouiture()

    'End Sub


End Class
